<template>
	<view class="coupon-container">
		<!-- 可领取的优惠券 -->
		<view class="coupon-item">
			<view v-for="(item,index) in couponList" :key="index" class="coupon-content">
				<view class="left-part">
					<view class="price-box">
						<text class="symbol">¥</text>
						<text class="price-num">{{item.reduce}}</text>
					</view>
					<view class="info-box">
						<text class="condition">满{{item.full}}元可用</text>
						<text class="desc">仅限于{{item.cheShop.shopName}}使用</text>
						<text class="date">{{item.startTime}}—{{item.endTime}}</text>
					</view>
				</view>
				<view class="right-part">
					<button v-if="item.state==uuuu"   class="get-btn" @click="getCoupon(item.id)">领取</button>
					<button v-else class="get-btn disabled">已领取</button>
				</view>
			</view>
		</view>


	</view>
</template>

<script>

	export default {
		data() {
			return {
				uuuu:0,
				couponList: [],
				couponData: {
					couponId: '',
					useid: ''
				}

			}
		},
		onLoad() {
			this.showCoupon()
		},
		onLaunch() {
			
		},
		methods: {
			showCoupon() {
				let that = this
				this.$http({
					url: '/api/shop/coupon/list',
					method: 'get',
					success(res) {
						that.couponList = res.data.rows
						
					}
				})
			},
			getUserIdFromCookie(cookie) {
				let that= this
				if (!cookie) return null;

				// 使用正则表达式匹配 userId 的值
				const match = cookie.match(/userId=([^;]*)/);
				return match ? match[1] : null; // 返回 userId 的值，如果不存在则返回 null
			},
			getCoupon(index) {
				const cookie = uni.getStorageSync('cookie')
				const userId = this.getUserIdFromCookie(cookie)
				this.couponData = {
					couponId: index,
					userid: userId
				}
				let that=this
				// 领取优惠券的逻辑
				this.$http({
					url: '/api/user/coupon_user',
					method: 'post',
					data: that.couponData,
					success(res) {
						if (res.data.code == 200) {
							uni.showToast({
								title:'领取成功',
								duration:1000,
								icon:'success'
							})
							that.couponData={
								couponId:'',
								userid:''
							}
							that.uuuu=1
						}else{
							uni.showToast({
								title:'领取失败',
								duration:1000,
								icon:'error'
							})
						}

					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.coupon-container {
		padding: 30rpx;
		background: #f5f5f5;
	}

	.coupon-item {
		margin-bottom: 20rpx;
		position: relative;

		.coupon-content {
			background: #FF6B35;
			border-radius: 12rpx;
			padding: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: relative;

			// 左边锯齿效果
			&::before {
				content: '';
				position: absolute;
				left: -2rpx;
				top: 10rpx;
				bottom: 10rpx;
				width: 4rpx;
				background-image: linear-gradient(#f5f5f5 50%, transparent 0);
				background-size: 4rpx 16rpx;
			}

			// 右边锯齿效果
			&::after {
				content: '';
				position: absolute;
				right: -2rpx;
				top: 10rpx;
				bottom: 10rpx;
				width: 4rpx;
				background-image: linear-gradient(#f5f5f5 50%, transparent 0);
				background-size: 4rpx 16rpx;
			}
		}

		&.disabled {
			.coupon-content {
				background: #FF6B35;
			}

			.price-box {
				color: #fff;
			}

			.info-box {
				.condition {
					color: #fff;
				}
			}

			.get-btn {
				background: #eee;
				color: #999;
			}
		}
	}

	.left-part {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.price-box {
		color: #fff;
		display: flex;
		align-items: baseline;
		margin-right: 30rpx;

		.symbol {
			font-size: 32rpx;
		}

		.price-num {
			font-size: 72rpx;
			font-weight: bold;
			line-height: 1;
		}
	}

	.info-box {
		display: flex;
		flex-direction: column;

		.condition {
			color: #fff;
			font-size: 32rpx;
			font-weight: 500;
			margin-bottom: 10rpx;
		}

		.desc {
			color: rgba(255, 255, 255, 0.8);
			font-size: 24rpx;
			margin-bottom: 6rpx;
		}

		.date {
			color: rgba(255, 255, 255, 0.6);
			font-size: 24rpx;
		}
	}

	.right-part {
		.get-btn {
			background: #fff;
			color: #FF6B35;
			font-size: 28rpx;
			padding: 12rpx 36rpx;
			border-radius: 40rpx;
			border: none;
			font-weight: 500;

			&.disabled {
				background: #eee;
				color: #999;
			}

			&::after {
				border: none;
			}
		}
	}
</style>